<?php
namespace Home\Controller;
use Think\Controller;

  
 
class GiftController extends Controller {


   //送出的礼物页面
	public function index(){
			$UserId = I('session.menuid');
			if($UserId){
			$default_begin_time = date("Y-m-d",strtotime("-3 Days")); 
			$default_end_time =date("Y-m-d",time());
			$startTime = I('get.start_date',$default_begin_time);		
			$preSevenDays = strtotime("-7 Days");
			
			if(strtotime($startTime)<$preSevenDays){
				$startTime = date("Y-m-d", $preSevenDays); 
			}
			
			$endTime = I('get.end_date',$default_end_time);
			
			if(strtotime($endTime)>time()){
				$endTime = $default_end_time; 
			}	
					
			$recieverId = I('get.recieverId','','htmlspecialchars');
			$giftName = I('get.giftname','','htmlspecialchars');
			$roomId = I('get.roomId','','htmlspecialchars');
			
			if(!empty($recieverId)){
					$condition['nbuddyid'] =$recieverId ; 
				}					

			if(!empty($giftName)){
				$condition['cgiftname'] =$giftName ;   		
			}	
			if(!empty($roomId)){
				$condition['nroomid'] =$roomId ;  	
			}

			$condition['nuserid'] =$UserId ;
						
			$condition['dtime'] = array('BETWEEN',array($startTime,date('Y-m-d',strtotime("$endTime +1 days"))));		
			
			$G = M('tbusertradegift');
			$count =$G->field('nuserid,cgiftname,nbuddyid,ncount,nmoney,nroomid,dtime,ngiftmoney')
					  ->where($condition)
					  ->count();
			$items =15;
			$Page = new \Think\Userpage($count,$items);
			
			$data = $G->field('nuserid,cgiftname,nbuddyid,ncount,nmoney,nroomid,dtime,ngiftmoney')
					  ->where($condition)
					  ->limit($Page->firstRow.','.$Page->listRows)
					  ->order('dtime desc')
					  ->select();
			$listshow = $Page->show(); 
			$this->assign('data',$data);
			$this->assign('startTime',I('get.start_date',$default_begin_time));
			
			$this->assign('endTime',I('get.end_date',$default_end_time));
			
			$this->assign('recieverId',$recieverId);
			
			$this->assign('giftName',$giftName);
			
			$this->assign('roomId',$roomId);
			
			$this->assign('list',$listshow);
			}

			$this->display();  
	
	}
	
	  //收到的礼物页面
	public function mygift(){
			$UserId = I('session.menuid');
			if($UserId){

			$default_begin_time = date("Y-m-d",strtotime("-3 Days")); 
			$default_end_time =date("Y-m-d",time());
			$startTime = I('get.start_date',$default_begin_time);		
			$preSevenDays = strtotime("-7 Days");
			
			if(strtotime($startTime)<$preSevenDays){
				$startTime = date("Y-m-d", $preSevenDays); 
			}
			
			$endTime = I('get.end_date',$default_end_time);
			
			if(strtotime($endTime)>time()){
				$endTime = $default_end_time; 
			}	
					
			$recieverId = I('get.recieverId','','htmlspecialchars');
			$giftName = I('get.giftname','','htmlspecialchars');
			$roomId = I('get.roomId','','htmlspecialchars');
			
			if(!empty($recieverId)){
				$condition['nuserid'] =$recieverId ; 
			}			

			if(!empty($giftName)){
				$condition['cgiftname'] =$giftName ;   		
			}	
			if(!empty($roomId)){
				$condition['nroomid'] =$roomId ;  	
			}
			$condition['nbuddyid'] =$UserId ;

			
			$condition['dtime'] = array('BETWEEN',array($startTime,date('Y-m-d',strtotime("$endTime +1 days"))));		
			
			$G = M('tbusertradegift');
			$count =$G->field('nuserid,cgiftname,nbuddyid,ncount,nmoney,nroomid,dtime,ngiftmoney')
					  ->where($condition)
					  ->count();
			$items =15;
			$Page = new \Think\Userpage($count,$items);
			
			$data = $G->field('nuserid,cgiftname,nbuddyid,ncount,nmoney,nroomid,dtime,ngiftmoney')
					  ->where($condition)
					  ->limit($Page->firstRow.','.$Page->listRows)
					  ->order('dtime desc')
					  ->select();
			$listshow = $Page->show(); 
			$this->assign('data',$data);
			$this->assign('list',$listshow);
			$this->assign('startTime',I('get.start_date',$default_begin_time));
			
			$this->assign('endTime',I('get.end_date',$default_end_time));
			
			$this->assign('recieverId',$recieverId);
			
			$this->assign('giftName',$giftName);
			
			$this->assign('roomId',$roomId);
			}
			$this->display();  	
	
	}
	//礼金流水
	public function giftflow(){		
			$UserId = I('session.menuid');
			$default_begin_time = date("Y-m-d",strtotime("-3 Days")); 
			$default_end_time =date("Y-m-d",time());
			$startTime = I('get.start_date',$default_begin_time);		
			$preSevenDays = strtotime("-7 Days");
			
			if(strtotime($startTime)<$preSevenDays){
				$startTime = date("Y-m-d", $preSevenDays); 
			}			
			$endTime = I('get.end_date',$default_end_time);			
			if(strtotime($endTime)>time()){
				$endTime = $default_end_time; 
			}
			$this->assign('startTime',I('get.start_date',$default_begin_time));			
			$this->assign('endTime',I('get.end_date',$default_end_time));		
			$condition['nuserid']=$UserId;
			$condition['dtime'] = array('BETWEEN',array($startTime,date("Y-m-d", strtotime("$endTime+1 days"))));
			$G = M('tbusergiftmoneyflow');
			$count =$G->where($condition)
					  ->count();
			$items =15;
			$Page = new \Think\Userpage($count,$items);
					
			$data = $G->field('dtime,cbuddy,nbuddyid,nmoney,nbalance,cdesc')
					  ->where($condition)
					  ->order('dtime DESC')
					  ->limit($Page->firstRow.','.$Page->listRows)	
					  ->select();
			$listshow = $Page->show(); 
			$this->assign('list',$listshow);
			$this->assign('data',$data);				
			$this->display();		
		
	}	
}